/**
 * 格式化日期
 * @param date 日期字符串或Date对象
 * @returns 格式化后的日期字符串
 */
export const formatDate = (date: string | Date): string => {
    const d = new Date(date)
    const year = d.getFullYear()
    const month = String(d.getMonth() + 1).padStart(2, '0')
    const day = String(d.getDate()).padStart(2, '0')
    const hour = String(d.getHours()).padStart(2, '0')
    const minute = String(d.getMinutes()).padStart(2, '0')

    return `${year}-${month}-${day} ${hour}:${minute}`
}

/**
 * 格式化距离现在的时间
 * @param date 日期字符串或Date对象
 * @returns 相对时间字符串
 */
export const formatTimeAgo = (date: string | Date): string => {
    const now = new Date()
    const d = new Date(date)
    const diff = now.getTime() - d.getTime()

    const minute = 60 * 1000
    const hour = minute * 60
    const day = hour * 24
    const month = day * 30
    const year = day * 365

    if (diff < minute) {
        return '刚刚'
    } else if (diff < hour) {
        return `${Math.floor(diff / minute)} 分钟前`
    } else if (diff < day) {
        return `${Math.floor(diff / hour)} 小时前`
    } else if (diff < month) {
        return `${Math.floor(diff / day)} 天前`
    } else if (diff < year) {
        return `${Math.floor(diff / month)} 个月前`
    } else {
        return `${Math.floor(diff / year)} 年前`
    }
}

/**
 * 格式化金额
 * @param amount 金额数值
 * @param decimals 小数位数
 * @returns 格式化后的金额字符串
 */
export const formatAmount = (amount: number, decimals = 2): string => {
    return amount.toLocaleString('zh-CN', {
        style: 'currency',
        currency: 'CNY',
        minimumFractionDigits: decimals,
        maximumFractionDigits: decimals
    })
}

export default {
    formatDate,
    formatTimeAgo,
    formatAmount
} 